package com.ruoyi.web.controller.recommend.counsellor;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.student.domain.StudentBaseInfo;
import com.ruoyi.student.domain.StudentRecommendInfo;
import com.ruoyi.student.domain.vo.*;
import com.ruoyi.student.service.IStudentBaseInfoService;
import com.ruoyi.student.service.IStudentRecommendInfoService;
import com.ruoyi.student.service.IStudentService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController("CounsellorStudentRecommendManageController")
@RequestMapping("/counsellor/student/recommend")
public class CounsellorStudentRecommendManageController extends BaseController {

    @Resource
    private IStudentBaseInfoService studentBaseInfoService;


    @Resource
    private IStudentRecommendInfoService studentRecommendInfoService;

    /**
     * 学生操作（多表操作）
     */
    @Resource
    private IStudentService studentService;


    /**
     * 获取学生推荐表信息列表
     *
     * @param vo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('counsellor:student:recommend:list')")
    @Log(title = "辅导员-查询学生推荐表信息列表", operatorType = OperatorType.MANAGE, businessType = BusinessType.QUERY)
    @GetMapping("/list")
    public TableDataInfo list(StudentRecommendListSearchVo vo) {
        LoginUser loginUser = getLoginUser();
        vo.setCounsellorCode(loginUser.getUsername());
        startPage();
        List<StudentRecommendListVo> list = studentService.selectStudentRecommendList(vo);
        return getDataTable(list);
    }




    /**
     * 获取学生推荐表信息
     *
     * @param code
     * @return
     */
    @PreAuthorize("@ss.hasPermi('counsellor:student:recommend:info')")
    @Log(title = "辅导员-查询单个学生推荐表信息", operatorType = OperatorType.MANAGE, businessType = BusinessType.QUERY)
    @GetMapping("/info/{code}")
    public AjaxResult info(@PathVariable("code") String code) {
        if (StringUtils.isEmpty(code)) {
            return error("无效学号参数");
        }
        //判断当前学生是否属于当前辅导员
        LoginUser loginUser = getLoginUser();
        //判断当前学生是否属于当前辅导员
        StudentBaseInfo studentBaseInfo = studentBaseInfoService.selectStudentBaseInfoByCode(code);
        if (studentBaseInfo == null) {
            return error("学号参数不正确");
        }
        if (!studentBaseInfo.getCounsellorCode().equals(loginUser.getUsername())) {
            return error("暂无权限");
        }

        StudentRecommendInfo studentRecommendInfo = studentRecommendInfoService.selectStudentRecommendInfoByCode(code);
        SelectStudentRecommendInfoVo selectStudentRecommendInfoVo = new SelectStudentRecommendInfoVo();

        selectStudentRecommendInfoVo.setName(studentBaseInfo.getName());
        selectStudentRecommendInfoVo.setCode(studentBaseInfo.getCode());
        selectStudentRecommendInfoVo.setPoliticalStatus(studentBaseInfo.getPoliticalStatus());
        selectStudentRecommendInfoVo.setForeignLanguageEvel(studentRecommendInfo.getForeignLanguageEvel());
        selectStudentRecommendInfoVo.setComputerLevel(studentRecommendInfo.getComputerLevel());
        selectStudentRecommendInfoVo.setJobIntention(studentRecommendInfo.getJobIntention());


        selectStudentRecommendInfoVo.setIsHkInSchool(studentRecommendInfo.getIsHkInSchool());//户口是否转入学校
        selectStudentRecommendInfoVo.setIsCityStudent(studentRecommendInfo.getIsCityStudent());//城乡生源
        selectStudentRecommendInfoVo.setSource(studentBaseInfo.getSource());//生源所在地

        selectStudentRecommendInfoVo.setPhoneNumber(studentBaseInfo.getPhoneNumber());
        selectStudentRecommendInfoVo.setQq(studentRecommendInfo.getQq());
        selectStudentRecommendInfoVo.setEmail(studentRecommendInfo.getEmail());


        selectStudentRecommendInfoVo.setHomeAddress(studentRecommendInfo.getHomeAddress());
        selectStudentRecommendInfoVo.setHomeCode(studentRecommendInfo.getHomeCode());
        selectStudentRecommendInfoVo.setHomePhone(studentRecommendInfo.getHomePhone());


        selectStudentRecommendInfoVo.setRxqdaszdw(studentRecommendInfo.getRxqdaszdw());
        selectStudentRecommendInfoVo.setRxqhkszdpcs(studentRecommendInfo.getRxqhkszdpcs());


        selectStudentRecommendInfoVo.setRewardPunishmentPrize1(studentRecommendInfo.getRewardPunishmentPrize1());
        selectStudentRecommendInfoVo.setRewardPunishmentPrize2(studentRecommendInfo.getRewardPunishmentPrize2());
        selectStudentRecommendInfoVo.setRewardPunishmentPrize3(studentRecommendInfo.getRewardPunishmentPrize3());
        selectStudentRecommendInfoVo.setRewardPunishmentPrize4(studentRecommendInfo.getRewardPunishmentPrize4());
        selectStudentRecommendInfoVo.setRewardPunishmentPrize5(studentRecommendInfo.getRewardPunishmentPrize5());

        selectStudentRecommendInfoVo.setWorkContent1(studentRecommendInfo.getWorkContent1());
        selectStudentRecommendInfoVo.setWorkContent2(studentRecommendInfo.getWorkContent2());
        selectStudentRecommendInfoVo.setWorkContent3(studentRecommendInfo.getWorkContent3());
        selectStudentRecommendInfoVo.setWorkContent4(studentRecommendInfo.getWorkContent4());
        selectStudentRecommendInfoVo.setWorkContent5(studentRecommendInfo.getWorkContent5());
        selectStudentRecommendInfoVo.setQualification(studentRecommendInfo.getQualification());

        selectStudentRecommendInfoVo.setDepartmentOpinion(studentRecommendInfo.getDepartmentOpinion());

        selectStudentRecommendInfoVo.setStudentRecommendSubmit(studentRecommendInfo.getStudentRecommendSubmit());
        selectStudentRecommendInfoVo.setCounselorRecommendCheck(studentRecommendInfo.getCounselorRecommendCheck());

        return success(selectStudentRecommendInfoVo);
    }


    /**
     * 更新学生的辅导员审核状态
     *
     * @param vo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('counsellor:student:recommend:check')")
    @Log(title = "辅导员-修改学生审核状态", operatorType = OperatorType.MANAGE, businessType = BusinessType.UPDATE)
    @PutMapping("/updateCheck")
    public AjaxResult updateCounsellorCheck(@RequestBody @Valid UpdateStudentRecommendCounsellorCheck vo) {
        LoginUser loginUser = getLoginUser();
        //判断当前学生是否属于当前辅导员
        StudentBaseInfo studentBaseInfo = studentBaseInfoService.selectStudentBaseInfoByCode(vo.getCode());
        if (studentBaseInfo == null) {
            return error("学号参数不正确");
        }
        if (!studentBaseInfo.getCounsellorCode().equals(loginUser.getUsername())) {
            return error("暂无权限");
        }
        if (vo.getStatus().equals("0")) {
            StudentRecommendInfo studentRecommendInfo = studentRecommendInfoService.selectStudentRecommendInfoByCode(vo.getCode());
            if (studentRecommendInfo.getStudentRecommendSubmit().equals("1")) {
                return error("该生还未提交推荐表信息");
            }
            if (StringUtils.isEmpty(studentRecommendInfo.getDepartmentOpinion())) {
                return error("该生还未填写所在系意见");
            }
        }
        return success(studentRecommendInfoService.updateStudentRecommendCounsellorCheck(vo.getCode(), vo.getStatus()));
    }


    /**
     * 填写学生所在系意见
     *
     * @param vo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('counsellor:student:recommend:check')")
    @Log(title = "辅导员-填写学生所在系意见", operatorType = OperatorType.MANAGE, businessType = BusinessType.UPDATE)
    @PutMapping("/fillOpinion")
    public AjaxResult fillDepartmentOpinion(@RequestBody @Valid FillStudentRecommendDepartmentOpinion vo) {
        LoginUser loginUser = getLoginUser();
        //判断当前学生是否属于当前辅导员
        StudentBaseInfo studentBaseInfo = studentBaseInfoService.selectStudentBaseInfoByCode(vo.getCode());
        if (studentBaseInfo == null) {
            return error("学号参数不正确");
        }
        if (!studentBaseInfo.getCounsellorCode().equals(loginUser.getUsername())) {
            return error("暂无权限");
        }
        return success(studentRecommendInfoService.fillStudentRecommendDepartmentOpinion(vo.getCode(), vo.getOpinion()));
    }


    /**
     * 填写学生所在系意见并且审核学生
     *
     * @param vo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('counsellor:student:recommend:check')")
    @Log(title = "辅导员-填写学生所在系意见并且审核学生", operatorType = OperatorType.MANAGE, businessType = BusinessType.UPDATE)
    @PutMapping("/fillOpinion/check")
    public AjaxResult fillDepartmentOpinionAndCheck(@RequestBody @Valid FillStudentRecommendDepartmentOpinion vo) {
        LoginUser loginUser = getLoginUser();
        //判断当前学生是否属于当前辅导员
        StudentBaseInfo studentBaseInfo = studentBaseInfoService.selectStudentBaseInfoByCode(vo.getCode());
        if (studentBaseInfo == null) {
            return error("学号参数不正确");
        }
        if (!studentBaseInfo.getCounsellorCode().equals(loginUser.getUsername())) {
            return error("暂无权限");
        }
        //更新当前学生推荐表信息采集的辅导员审核为已审核
        studentRecommendInfoService.updateStudentRecommendCounsellorCheck(vo.getCode(), "0");
        return success(studentRecommendInfoService.fillStudentRecommendDepartmentOpinion(vo.getCode(), vo.getOpinion()));
    }


}
